﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace QLyHocSinhTHPT
{
    public partial class HocSinh : DevComponents.DotNetBar.Office2007Form
    {
        HSinh hs = new HSinh();
        public HocSinh()
        {
            InitializeComponent();
        }

       public SqlConnection GetConnect()
       {
          return new SqlConnection(@"Data Source=HOANGNGOC-PC\HOANGNGOC;Initial Catalog=QLyHocSinhTHPT;Integrated Security=True");
       }

       public DataTable HocSinh_Search(HSinh hs)
       {
           SqlConnection con = GetConnect();
           con.Open();
           SqlCommand cmd = new SqlCommand("sp_HocSinh_Search", con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@MaHS", hs.MaHS));
           cmd.Parameters.Add(new SqlParameter("@TenHS", hs.TenHS));
           SqlDataAdapter adap = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           adap.Fill(dt);
           con.Close();
           return dt;
       }

       public void HocSinh_Delete(string mahs)
       {
           SqlConnection con = GetConnect();
           con.Open();
           SqlCommand cmd = new SqlCommand("sp_HocSinh_Delete", con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@MaHS", mahs));
           cmd.ExecuteNonQuery();
           con.Close();
       }

       public DataTable HocSinh_SelectAll()
       {
           SqlConnection con = GetConnect();
           con.Open();
           SqlCommand cmd = new SqlCommand("sp_HocSinh_GetByAll", con);
           cmd.CommandType = CommandType.StoredProcedure;
           SqlDataAdapter adap = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           adap.Fill(dt);
           con.Close();
           return dt;
       }

       public void HocSinh_Insert(HSinh hs)
       {
           SqlConnection con = GetConnect();
           con.Open();
           SqlCommand cmd = new SqlCommand("sp_HocSinh_Insert", con);
           cmd.CommandType = CommandType.StoredProcedure;

           cmd.Parameters.Add(new SqlParameter("@MaHS", hs.MaHS));
           cmd.Parameters.Add(new SqlParameter("@HoDem", hs.HoDem));
           cmd.Parameters.Add(new SqlParameter("@TenHS", hs.TenHS));
           cmd.Parameters.Add(new SqlParameter("@NgaySinh", hs.NgaySinh));
           cmd.Parameters.Add(new SqlParameter("@GioiTinh", hs.GioiTinh));
           cmd.Parameters.Add(new SqlParameter("@DiaChi", hs.DiaChi));
           cmd.Parameters.Add(new SqlParameter("@Malop", hs.Malop));

           cmd.ExecuteNonQuery();
           con.Close();
       }

       public void HocSinh_Update(HSinh hs)
       {
           SqlConnection con = GetConnect();
           con.Open();
           SqlCommand cmd = new SqlCommand("sp_HocSinh_Update", con);
           cmd.CommandType = CommandType.StoredProcedure;

           cmd.Parameters.Add(new SqlParameter("@MaHS", hs.MaHS));
           cmd.Parameters.Add(new SqlParameter("@HoDem", hs.HoDem));
           cmd.Parameters.Add(new SqlParameter("@TenHS", hs.TenHS));
           cmd.Parameters.Add(new SqlParameter("@NgaySinh", hs.NgaySinh));
           cmd.Parameters.Add(new SqlParameter("@GioiTinh", hs.GioiTinh));
           cmd.Parameters.Add(new SqlParameter("@DiaChi", hs.DiaChi));
           cmd.Parameters.Add(new SqlParameter("@Malop", hs.Malop));

           cmd.ExecuteNonQuery();
           con.Close();
       }


        private void panelEx2_Click(object sender, EventArgs e)
        {

        }

        private void labelX3_Click(object sender, EventArgs e)
        {

        }

        private void textBoxX8_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxX4_TextChanged(object sender, EventArgs e)
        {

        }

           
        private void btnthem_Click(object sender, EventArgs e)
        {

            if (txtmahs.Text.Trim().Equals(" "))
            {
                MessageBox.Show("ban phai nhap ma hoc sinh");
                txtmahs.Focus();
                return;
            }

            HSinh hs = new HSinh();
            hs.MaHS = txtmahs.Text;
            hs.HoDem = txthodem.Text;
            hs.TenHS = txttenhs.Text;
            hs.NgaySinh = dtNS.Value;
            hs.GioiTinh = int.Parse( txtgioitinh.Text);
            hs.DiaChi = txtdiachi.Text;
            hs.Malop = txtmalop.Text;

            try
            {
                HocSinh_Insert(hs);
                dgvhocsinh.DataSource = HocSinh_SelectAll();
            }
            catch

            {
                MessageBox.Show("chua them duoc");
            }

        }

        private void btnsua_Click(object sender, EventArgs e)
        {

            if (txtmahs.Text.Trim().Equals(" "))
            {
                MessageBox.Show("ban phai nhap ma hoc sinh");
                txtmahs.Focus();
                return;
            }

            HSinh hs = new HSinh();
            hs.MaHS = txtmahs.Text;
            hs.HoDem = txthodem.Text;
            hs.TenHS = txttenhs.Text;
            hs.NgaySinh = dtNS.Value;
            hs.GioiTinh = int.Parse(txtgioitinh.Text);
            hs.DiaChi = txtdiachi.Text;
            hs.Malop = txtmalop.Text;

             HocSinh_Update(hs);
             dgvhocsinh.DataSource = HocSinh_SelectAll();
          
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (txtmahs.Text.Trim().Equals(" "))
            {
                MessageBox.Show("Ban phai nhap ma nhan vien");
                txtmahs.Focus();
                return;
            }

            string mahs;
            mahs = txtmahs.Text;
            HocSinh_Delete(mahs);
            dgvhocsinh.DataSource = HocSinh_SelectAll();
        }

        private void btnfind_Click(object sender, EventArgs e)
        {

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void dgvhocsinh_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }
       

        

        private void dgvhocsinh_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void txttimkiem_TextChanged_1(object sender, EventArgs e)
        {
            HSinh hs = new HSinh();
            hs.MaHS = txttimkiem.Text;
            hs.TenHS = txttimkiem.Text;
            dgvhocsinh.DataSource = HocSinh_Search(hs);
        }

        private void HocSinh_Load(object sender, EventArgs e)
        {
            dgvhocsinh.DataSource = HocSinh_SelectAll();
        }

        private void dgvhocsinh_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
             int RowIndex = e.RowIndex;
            txtmahs.Text = dgvhocsinh.Rows[RowIndex].Cells[0].Value.ToString();
            txthodem.Text = dgvhocsinh.Rows[RowIndex].Cells[1].Value.ToString();
            txttenhs.Text = dgvhocsinh.Rows[RowIndex].Cells[2].Value.ToString();
            dtNS.Value = DateTime.Parse(dgvhocsinh.Rows[RowIndex].Cells[3].Value.ToString());
            txtgioitinh.Text = dgvhocsinh.Rows[RowIndex].Cells[4].Value.ToString();
            txtdiachi.Text = dgvhocsinh.Rows[RowIndex].Cells[5].Value.ToString();
            txtmalop.Text = dgvhocsinh.Rows[RowIndex].Cells[6].Value.ToString();
        }

      
        
       
    }
}
